/**
  ******************************************************************************
  * @file    _tc32l010_lpuart.h
  * @author  CHIPAT Application Team
  * @brief   This file contains all the functions prototypes for the LPTIM
  *          firmware library
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __XS32L010_LPUART_H
#define __XS32L010_LPUART_H

#ifdef __cplusplus
 extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "tc32l010.h"
#include "tc32l010_rcc.h"

/** @addtogroup XS32L010_StdPeriph_Driver
  * @{
  */

/** @addtogroup LPUART
  * @{
  */

/* Exported types ------------------------------------------------------------*/

/** @defgroup LPUART_Exported_Types LPUART Exported Types
  * @{
  */

/** 
  * @brief  LPUART Init structure definition
  */

/** 
  * @brief  LPUART Init Structure definition  
  */ 
  
typedef struct
{
  uint32_t LPUART_BaudRate;            /*!< This member configures the LPUART communication baud rate.
                                           The baud rate is computed using the following formula:
                                            - BRR = ((PCLKx) / (LPUART_InitStruct->LPUART_BaudRate) + 0.5 */

  uint16_t LPUART_WordLength;          /*!< Specifies the number of data bits transmitted or received in a frame.
                                           This parameter can be a value of @ref LPUART_Word_Length */

  uint16_t LPUART_StopBits;            /*!< Specifies the number of stop bits transmitted.
                                           This parameter can be a value of @ref LPUART_Stop_Bits */

  uint16_t LPUART_Parity;              /*!< Specifies the parity mode.
                                           This parameter can be a value of @ref LPUART_Parity
                                           @note When parity is enabled, the computed parity is inserted
                                                 at the MSB position of the transmitted data (9th bit when
                                                 the word length is set to 9 data bits; 8th bit when the
                                                 word length is set to 8 data bits). */
 
  uint16_t LPUART_Mode;                /*!< Specifies whether the Receive or Transmit mode is enabled or disabled.
                                           This parameter can be a value of @ref LPUART_Mode */

  uint16_t LPUART_HardwareFlowControl; /*!< Specifies wether the hardware flow control mode is enabled
                                           or disabled.
                                           This parameter can be a value of @ref LPUART_Hardware_Flow_Control */

  uint8_t LPUART_FirstBit;             /*!< Specifies whether data transfers start from MSB or LSB bit.
                                         This parameter can be a value of @ref SPI_MSB_LSB_transmission */

  uint8_t LPUART_DataInv;              /*!< Binary data inversion */

  uint8_t LPUART_TxPinInv;             /*!< Tx Pin active level inversion */

  uint8_t LPUART_RxPinInv;             /*!< Rx Pin active level inversion */

  uint8_t LPUART_SwapTxRx;             /*!< Swap Tx/Rx Pins */
  
} LPUART_InitTypeDef;



#define IS_LPUART_ALL_PERIPH(PERIPH)                  (((PERIPH) == LPUART0))


/** @defgroup LPUART_Word_Length 
  * @{
  */ 
  
#define LPUART_WordLength_8b                  ((uint16_t)0x0000)
#define LPUART_WordLength_9b                  ((uint16_t)0x1000)
                                    
#define IS_LPUART_WORD_LENGTH(LENGTH) (((LENGTH) == LPUART_WordLength_8b) || \
                                      ((LENGTH) == LPUART_WordLength_9b))
/**
  * @}
  */ 

/** @defgroup LPUART_Stop_Bits 
  * @{
  */ 
  
#define LPUART_StopBits_1                     ((uint16_t)0x0000)
#define LPUART_StopBits_2                     ((uint16_t)0x2000)
#define IS_LPUART_STOPBITS(STOPBITS) (((STOPBITS) == LPUART_StopBits_1) || \
                                      ((STOPBITS) == LPUART_StopBits_2))
/**
  * @}
  */ 

/** @defgroup LPUART_Parity 
  * @{
  */ 
  
#define LPUART_Parity_No                      ((uint16_t)0x0000)
#define LPUART_Parity_Even                    ((uint16_t)0x0400)
#define LPUART_Parity_Odd                     ((uint16_t)0x0600) 
#define IS_LPUART_PARITY(PARITY) (((PARITY) == LPUART_Parity_No) || \
                                 ((PARITY) == LPUART_Parity_Even) || \
                                 ((PARITY) == LPUART_Parity_Odd))
/**
  * @}
  */ 

/** @defgroup LPUART_Mode 
  * @{
  */ 
  
#define LPUART_Mode_Rx                        ((uint16_t)0x0004)
#define LPUART_Mode_Tx                        ((uint16_t)0x0008)
#define IS_LPUART_MODE(MODE) ((((MODE) & (uint16_t)0xFFF3) == 0x00) && ((MODE) != (uint16_t)0x00))
/**
  * @}
  */ 

/** @defgroup LPUART_Hardware_Flow_Control 
  * @{
  */ 
#define LPUART_HardwareFlowControl_None       ((uint16_t)0x0000)
#define LPUART_HardwareFlowControl_RTS        ((uint16_t)0x0100)
#define LPUART_HardwareFlowControl_CTS        ((uint16_t)0x0200)
#define LPUART_HardwareFlowControl_RTS_CTS    ((uint16_t)0x0300)
#define IS_LPUART_HARDWARE_FLOW_CONTROL(CONTROL)\
                              (((CONTROL) == LPUART_HardwareFlowControl_None) || \
                               ((CONTROL) == LPUART_HardwareFlowControl_RTS) || \
                               ((CONTROL) == LPUART_HardwareFlowControl_CTS) || \
                               ((CONTROL) == LPUART_HardwareFlowControl_RTS_CTS))
/**
  * @}
  */ 

/** @defgroup LPUART_MSB_LSB_transmission 
  * @{
  */

#define LPUART_FirstBit_MSB                 ((uint8_t)0x01)
#define LPUART_FirstBit_LSB                 ((uint8_t)0x00)
#define IS_LPUART_FIRST_BIT(BIT) (((BIT) == LPUART_FirstBit_MSB) || \
                                  ((BIT) == LPUART_FirstBit_LSB))

/** @defgroup LPUART_DataInversion 
  * @{
  */ 
  
#define LPUART_DataInversion_Disable              ((uint8_t)0x00)
#define LPUART_DataInversion_Enable               ((uint8_t)0x01)
#define IS_LPUART_DATAINV(DATAINV) ((((DATAINV) == LPUART_DataInversion_Disable) || \
                                    ((DATAINV) == LPUART_DataInversion_Enable))
/**
  * @}
  */ 

 /** @defgroup LPUART_TxPin inversion 
  * @{
  */ 
  
#define LPUART_TxInversion_Disable                   ((uint8_t)0x00)
#define LPUART_TxInversion_Enable                    ((uint8_t)0x01)
#define IS_LPUART_TXINV(TXINV)    ((((TXINV) == LPUART_TxInversion_Disable) || \
                                   ((TXINV) == LPUART_TxInversion_Enable))
/**
  * @}
  */

 /** @defgroup LPUART_RxPin inversion 
  * @{
  */ 
  
#define LPUART_RxInversion_Disable                   ((uint8_t)0x00)
#define LPUART_RxInversion_Enable                    ((uint8_t)0x01)
#define IS_LPUART_RXINV(RXINV)    ((((RXINV) == LPUART_RxInversion_Disable) || \
                                   ((RXINV) == LPUART_RxInversion_Enable))
/**
  * @}
  */  

 /** @defgroup LPUART_Swap Tx\RxPin
  * @{
  */ 
  
#define LPUART_SwapTxRx_Disable                   ((uint8_t)0x00)
#define LPUART_SwapTxRx_Enable                    ((uint8_t)0x01)
#define IS_LPUART_SWAP_EN(EN)    ((((EN) == LPUART_SwapTxRx_Disable) || \
                                   ((EN) == LPUART_SwapTxRx_Enable))
/**
  * @}
  */  

/**
  * @}
  */  

/** @defgroup LPUART_AddressWidth 
  * @{
  */ 
#define LPUART_AddressWidth_4Bits            ((uint8_t)0x0000)
#define LPUART_AddressWidth_7Bits            ((uint8_t)0x0010)

#define IS_LPUART_ADDRESSWIDTH(WIDTH) (((WIDTH) == LPUART_AddressWidth_4Bits)||((WIDTH) == LPUART_AddressWidth_7Bits))

/** @defgroup LPUART_RS485_Driverenable_time
  * @{
  */
#define IS_LPUART_RS485DRIVER_ENABLE_TIME(TIME) (((TIME) <= 0x1F))

/** @defgroup LPUART_RS485
  * @{
  */
#define LPUART_RS485_DE_Polarity_High        ((uint8_t)0x00)
#define LPUART_RS485_DE_Polarity_Low         ((uint8_t)0x01)
#define IS_LPUART_RS485DRIVER_ENABLE_POLARITY(POLARITY) (((POLARITY) == LPUART_RS485_DE_Polarity_High) || \
                                                         ((POLARITY) == LPUART_RS485_DE_Polarity_Low))

#define LPUART_RS485_DE_Disable              ((uint8_t)0x00)
#define LPUART_RS485_DE_Enable               ((uint8_t)0x01)

#define IS_LPUART_RS485DRIVER_ENABLE_MODE(MODE)  (((MODE) == LPUART_RS485_DE_Disable) || \
                                                  ((MODE) == LPUART_RS485_DE_Enable))


/**
  * @}
  */
  
/** @defgroup LPUART_Interrupt_definition 
  * @{
  */
  
#define LPUART_IT_CM                          ((uint16_t)0x112E)
#define LPUART_IT_PE                          ((uint16_t)0x0028)
#define LPUART_IT_TXE                         ((uint16_t)0x0727)
#define LPUART_IT_TC                          ((uint16_t)0x0626)
#define LPUART_IT_RXNE                        ((uint16_t)0x0525)
#define LPUART_IT_ORE_RX                      ((uint16_t)0x0325) /* In case interrupt is generated if the RXNEIE bit is set */
#define LPUART_IT_IDLE                        ((uint16_t)0x0424)
#define LPUART_IT_WU                          ((uint16_t)0x1476)
#define LPUART_IT_CTSI                        ((uint16_t)0x096A)
#define LPUART_IT_ERR                         ((uint16_t)0x0060)
#define LPUART_IT_ORE_ER                      ((uint16_t)0x0360) /* In case interrupt is generated if the EIE bit is set */
#define LPUART_IT_NE                          ((uint16_t)0x0260)
#define LPUART_IT_FE                          ((uint16_t)0x0160)

/** @defgroup LPUART_Legacy 
  * @{
  */
#define LPUART_IT_ORE                          LPUART_IT_ORE_ER               
/**
  * @}
  */

#define IS_LPUART_CONFIG_IT(IT) (((IT) == LPUART_IT_PE) || ((IT) == LPUART_IT_TXE)  || \
                                 ((IT) == LPUART_IT_TC) || ((IT) == LPUART_IT_RXNE) || \
                                 ((IT) == LPUART_IT_IDLE) || \
                                 ((IT) == LPUART_IT_CTSI) || ((IT) == LPUART_IT_ERR))
#define IS_LPUART_GET_IT(IT) (((IT) == LPUART_IT_PE) || ((IT) == LPUART_IT_TXE)  || \
                              ((IT) == LPUART_IT_TC) || ((IT) == LPUART_IT_RXNE) || \
                              ((IT) == LPUART_IT_IDLE) || \
                              ((IT) == LPUART_IT_CTSI) || ((IT) == LPUART_IT_ORE) || \
                              ((IT) == LPUART_IT_ORE_RX) || ((IT) == LPUART_IT_ORE_ER) || \
                              ((IT) == LPUART_IT_NE) || ((IT) == LPUART_IT_FE))
#define IS_LPUART_CLEAR_IT(IT) (((IT) == LPUART_IT_TC) || ((IT) == LPUART_IT_RXNE) || \
                                ((IT) == LPUART_IT_CTSI)|| ((IT) == LPUART_IT_CM)   || \
                                ((IT) == LPUART_IT_WU) || ((IT) == LPUART_IT_IDLE))
/**
  * @}
  */

/** @defgroup LPUART_DMA_Requests 
  * @{
  */

#define LPUART_DMAReq_Tx                      ((uint16_t)0x0080)
#define LPUART_DMAReq_Rx                      ((uint16_t)0x0040)
#define IS_LPUART_DMAREQ(DMAREQ) ((((DMAREQ) & (uint16_t)0xFF3F) == 0x00) && ((DMAREQ) != (uint16_t)0x00))

/**
  * @}
  */ 

/** @defgroup LPUART_WakeUp_methods
  * @{
  */

#define LPUART_WakeUp_IdleLine                ((uint16_t)0x0000)
#define LPUART_WakeUp_AddressMark             ((uint16_t)0x0800)
#define IS_LPUART_WAKEUP(WAKEUP) (((WAKEUP) == LPUART_WakeUp_IdleLine) || \
                                 ((WAKEUP) == LPUART_WakeUp_AddressMark))

#define LPUART_WakeUp_From_AddrMatch          ((uint8_t)0x00)
#define LPUART_WakeUp_From_StartCheck         ((uint8_t)0x02)
#define LPUART_WakeUp_From_RXNE               ((uint8_t)0x03)
#define IS_LPUART_WAKEUP_FSTOP(WAKEUP) (((WAKEUP) == LPUART_WakeUp_From_AddrMatch) || \
                                        ((WAKEUP) == LPUART_WakeUp_From_StartCheck)|| \
                                        ((WAKEUP) == LPUART_WakeUp_From_RXNE))

/**
  * @}
  */
  
/** @defgroup LPUART_Flags 
  * @{
  */
#define LPUART_FLAG_REACK                     ((uint32_t)0x400000)
#define LPUART_FLAG_TEACK                     ((uint32_t)0x200000)
#define LPUART_FLAG_WU                        ((uint32_t)0x100000)
#define LPUART_FLAG_CM                        ((uint32_t)0x20000)
#define LPUART_FLAG_BUSY                      ((uint32_t)0x10000)
#define LPUART_FLAG_CTS                       ((uint32_t)0x0400)
#define LPUART_FLAG_CTSI                      ((uint32_t)0x0200)
#define LPUART_FLAG_TXE                       ((uint32_t)0x0080)
#define LPUART_FLAG_TC                        ((uint32_t)0x0040)
#define LPUART_FLAG_RXNE                      ((uint32_t)0x0020)
#define LPUART_FLAG_IDLE                      ((uint32_t)0x0010)
#define LPUART_FLAG_ORE                       ((uint32_t)0x0008)
#define LPUART_FLAG_NE                        ((uint32_t)0x0004)
#define LPUART_FLAG_FE                        ((uint32_t)0x0002)
#define LPUART_FLAG_PE                        ((uint32_t)0x0001)
#define IS_LPUART_FLAG(FLAG) (((FLAG) == LPUART_FLAG_PE)  || ((FLAG) == LPUART_FLAG_TXE) || \
                              ((FLAG) == LPUART_FLAG_TC)  || ((FLAG) == LPUART_FLAG_RXNE)|| \
                              ((FLAG) == LPUART_FLAG_IDLE)|| ((FLAG) == LPUART_FLAG_BUSY)|| \
                              ((FLAG) == LPUART_FLAG_CTSI)|| ((FLAG) == LPUART_FLAG_ORE) || \
                              ((FLAG) == LPUART_FLAG_NE)  || ((FLAG) == LPUART_FLAG_FE)  || \
                              ((FLAG) == LPUART_FLAG_CM)  || ((FLAG) == LPUART_FLAG_WU)  || \
                              ((FLAG) == LPUART_FLAG_REACK) || ((FLAG) == LPUART_FLAG_TEACK) || \
                              ((FLAG) == LPUART_FLAG_CTS))
                              
#define IS_LPUART_CLEAR_FLAG(FLAG) ((((FLAG) & (uint32_t)0xFFEDFD9F) == 0x00) && ((FLAG) != (uint32_t)0x00))

  
#define IS_LPUART_BAUDRATE(BAUDRATE) (((BAUDRATE) > 0) && ((BAUDRATE) < 96000001))
#define IS_LPUART_ADDRESS(ADDRESS) ((ADDRESS) <= 0xFF)
#define IS_LPUART_DATA(DATA) ((DATA) <= 0x1FF)

/* Exported macro ------------------------------------------------------------*/
/* Exported functions --------------------------------------------------------*/  

/*  Function used to set the LPUART configuration to the default reset state ***/ 
void LPUART_DeInit(LPUART_TypeDef* LPUARTx);
void LPUART_Init(LPUART_TypeDef* LPUARTx, LPUART_InitTypeDef* LPUART_InitStruct);
void LPUART_StructInit(LPUART_InitTypeDef* LPUART_InitStruct);
void LPUART_Cmd(LPUART_TypeDef* LPUARTx, FunctionalState NewState);
void LPUART_SendData(LPUART_TypeDef* LPUARTx, uint16_t Data);
uint16_t LPUART_ReceiveData(LPUART_TypeDef* LPUARTx);
void LPUART_SetAddress(LPUART_TypeDef* LPUARTx, uint8_t LPUART_Address, uint8_t LPUART_AddressWidth);
void LPUART_ReceiverWakeUpCmd(LPUART_TypeDef* LPUARTx, FunctionalState NewState);
void LPUART_WakeUpConfig(LPUART_TypeDef* LPUARTx, uint16_t LPUART_WakeUp);
void LPUART_StopModeCmd(LPUART_TypeDef* LPUARTx, FunctionalState NewState);
void LPUART_StopModeConfig(LPUART_TypeDef* LPUARTx, uint8_t LPUART_WakeUp);
void LPUART_RS485DriverEnableConfig(LPUART_TypeDef* LPUARTx, uint8_t LPUART_DEPolarity, uint8_t LPUART_DEMode);
void LPUART_RS485DriverEnableTimeConfig(LPUART_TypeDef* LPUARTx, uint8_t LPUART_AssertionTime, uint8_t LPUART_DeassertionTime);
void LPUART_SendBreak(LPUART_TypeDef* LPUARTx);
void LPUART_OVRCmd(LPUART_TypeDef* LPUARTx, FunctionalState NewState);
void LPUART_HalfDuplexCmd(LPUART_TypeDef* LPUARTx, FunctionalState NewState);
void LPUART_DMACmd(LPUART_TypeDef* LPUARTx, uint16_t LPUART_DMAReq, FunctionalState NewState);
void LPUART_ITConfig(LPUART_TypeDef* LPUARTx, uint32_t LPUART_IT, FunctionalState NewState);
FlagStatus LPUART_GetFlagStatus(LPUART_TypeDef* LPUARTx, uint32_t LPUART_FLAG);
void LPUART_ClearFlag(LPUART_TypeDef* LPUARTx, uint32_t LPUART_FLAG);
ITStatus LPUART_GetITStatus(LPUART_TypeDef* LPUARTx, uint32_t LPUART_IT);
void LPUART_ClearITPendingBit(LPUART_TypeDef* LPUARTx, uint32_t LPUART_IT);





/**
  * @}
  */

#ifdef __cplusplus
}
#endif

#endif /* __tc32l010_LPUART_H */

/************************ (C) COPYRIGHT CHIPAT *****END OF FILE****/
